<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import type { PropertyFee, PropertyFeeParams } from '@/types/property'
import { getPropertyFeeListAPI } from '@/apis/property'

// 列表数据
const feeList = ref<PropertyFee[]>([])
const total = ref(0)

// 查询参数
const queryParams = reactive<PropertyFeeParams>({
  page: 1,
  pageSize: 10,
  enterpriseName: '',
  paymentTime: []
})

// 获取列表数据
const getFeeList = async () => {
  // 处理日期范围
  const params = {
    ...queryParams,
    startTime: queryParams.paymentTime?.[0],
    endTime: queryParams.paymentTime?.[1]
  }
  // 删除原始日期数组，避免发送到后端
  delete params.paymentTime
  
  const res = await getPropertyFeeListAPI(params)
  feeList.value = res.data.rows
  total.value = res.data.total
}

// 查询
const handleQuery = () => {
  queryParams.page = 1
  getFeeList()
}

// 初始化加载
getFeeList()
</script>

<template>
  <div class="property-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :inline="true" :model="queryParams">
        <el-form-item label="企业名称">
          <el-input
            v-model="queryParams.enterpriseName"
            placeholder="请输入企业名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- 缴费时间组件 -->
         <el-form-item label="缴费时间">
          <el-date-picker
            v-model="queryParams.paymentTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          ></el-date-picker>
         </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加账单区域 -->
    <div class="add-button">
      <el-button type="primary">添加账单</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table-container">
      <el-table :data="feeList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column prop="billNumber" label="账单编号" align="center" />
        <el-table-column prop="enterpriseName" label="企业名称" align="center" />
        <el-table-column prop="buildingName" label="租赁楼宇" align="center" />
        <el-table-column label="物业费（元/㎡）" align="center">
          <template #default="{ row }">
            {{ row.propertyFeePrice }}元/㎡/月
          </template>
        </el-table-column>
        <el-table-column label="账单金额" align="center">
          <template #default="{ row }">
            {{ row.paymentAmount }}元
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="缴费时间" align="center" />
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button type="primary" link size="small">查看</el-button>
            <el-button type="primary" link size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="getFeeList"
        @current-change="getFeeList"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.property-container {
  padding: 20px;
  background-color: #fff;

  .search-container {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #fff;
    border-radius: 4px;
  }

  .add-button {
    margin: 15px 0;
  }

  .table-container {
    margin-bottom: 20px;
  }

  .pagination-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
  }
}
</style>
